package com.zndroid.gridview.inner.drag;

import android.view.View;

import com.zndroid.gridview.GridDroppableLayout;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;

/**
 * 每一项组合抽象成链表
 * */
public class Global {
    private LinkedHashMap<View, Item> container = new LinkedHashMap<>();
    private LinkedList<Item> mChildren = new LinkedList<>();
    private GridDroppableLayout parent;

    public Global(GridDroppableLayout parent) {
        this.parent = parent;
    }

    public void add(int index, View view) {
        Item child = container.get(view);
        if (child == null) {
            child = new Item(view);
            child.setParent(parent);
            container.put(view, child);
        }
        mChildren.add(index, child);
    }

    public boolean remove(Item child) {
        return mChildren.remove(child);
    }

    public void remove(int index) {
        mChildren.remove(index);
    }

    public Item get(int index) {
        return mChildren.get(index);
    }

    public int indexOf(View v) {
        Item child = container.get(v);
        if (child == null) {
            return -2;
        }
        return mChildren.indexOf(child);
    }

    public int size() {
        return mChildren.size();
    }

    public void clear() {
        container.clear();
        Iterator<Item> it = mChildren.iterator();
        //子view从gridView移除时取消动画效果
        while (it.hasNext()) {
            Item child = it.next();
            child.cancel();
            it.remove();
        }
    }
}
